package com.laicunba.ext.web.controller.h5.activity;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.laicunba.ext.service.SlaveActivityJoinService;
import com.laicunba.ext.service.SlaveBidAnalysisService;
import com.laicunba.ext.service.SlaveUserService;
import com.laicunba.ext.web.controller.SuiCunBaoBaseController;
import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.ActivityService;
import com.laicunba.service.PicService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.UserService;
import com.laicunba.service.WeiXinService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

@Controller
@RequestMapping(value = "/h5/ext/activity")
public class GoldMyInviteController extends SuiCunBaoBaseController {
	@Autowired
	private RegistryService registryService;
	@Autowired
	private WeiXinService weiXinService;
	@Autowired
	private UserService userService;
	@Autowired
	private PicService picService;
	@Value("${ftp.dir:/resource/}")
	private String ftpDir;
	@Autowired
	private SlaveUserService slaveUserService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private SlaveActivityJoinService slaveActivityJoinService;
	@Autowired
	private SlaveBidAnalysisService slaveBidAnalysisService;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/gold_my_invite.htm")
	public ModelAndView goldMyInvite(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("gold_my_invite.jsp");
		try {
			UserDO userDO = getCurrentUser(request);
			if (userDO == null) {
//				view = new ModelAndView("redirect:/../h5/login.htm?tab=account&returnUrl=/../h5/ext/activity/gold_my_invite.htm");
				return view;
			}
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			//查询今日剩余抽奖次数
			String todayTotalChance = CacheUtil.getAttribute("act.20170410chance." + userDO.getId() + DateUtil.formatDate(new Date()));
			int count = slaveActivityJoinService.findGainCount("20170410", userDO.getId(), sdf.format(new Date()), sdf.format(DateUtils.addDays(new Date(), 1)));
			if(StringUtil.isNotEmpty(todayTotalChance) && count >= 0) {
				view.addObject("remainGainCount", Integer.parseInt(todayTotalChance) - count);
			} else {
				view.addObject("remainGainCount", 0);
			}
			
			//查询累计邀请好友注册数和投资数
			ActivityDO activity = activityService.get(ActivityDO.class, "20170410");
			if(activity != null) {
				Map<String, Object> tempMap = null;
				List<Map<String, Object>> inviteRankList = slaveUserService.findInviteRankList(sdf.format(activity.getBeginTime()), sdf.format(DateUtils.addDays(activity.getEndTime(), 1)), 5);
				RegistryDO registry = registryService.get(RegistryDO.class, "sys.act.data.20170410");
				if(inviteRankList != null && registry != null && StringUtil.isNotEmpty(registry.getVal())) {
					JSONArray registryInviteArray = (JSONArray)new JSONParser().parse(registry.getVal());
					if(registryInviteArray != null && registryInviteArray.size() > 0) {
						for (int i = 0; i < registryInviteArray.size(); i++) {
							tempMap = (Map<String, Object>) registryInviteArray.get(i);
							inviteRankList.add(tempMap);
						}
						Collections.sort(inviteRankList, new Comparator<Map<String, Object>>() {
							public int compare(Map<String, Object> m1, Map<String, Object> m2) {
								if(Integer.valueOf(String.valueOf(m1.get("invite_count"))).compareTo(Integer.valueOf(String.valueOf(m2.get("invite_count")))) > 0) {
									return -1;
								} else {
									return 1;
								}
							}
						});
					}
					int myInviteRank = 0;
					for(int i = 0; i < inviteRankList.size(); i++) {
						if(i < 5 && userDO.getMobile().equals(inviteRankList.get(i).get("mobile"))) {
							myInviteRank = i + 1;
							break;
						}
					}
					view.addObject("myInviteRank", myInviteRank);
				}
				
				view.addObject("inviteRegistCount", slaveUserService.findInviteRegistCount(userDO.getId(), sdf.format(activity.getBeginTime()), sdf.format(DateUtils.addDays(activity.getEndTime(), 1))));
				view.addObject("inviteInvestCount", slaveBidAnalysisService.findInviteInvestCount(userDO.getId(), sdf.format(activity.getBeginTime()), sdf.format(DateUtils.addDays(activity.getEndTime(), 1))));
			} else {
				view.addObject("inviteRegistCount", 0);
				view.addObject("inviteInvestCount", 0);
			}
			
			//查询累计抽取黄金
			Map<String, Object> goldMyInviteData = slaveActivityJoinService.findTotalReward("20170410", userDO.getId());
			BigDecimal totalReward = new BigDecimal(0);
			if(goldMyInviteData != null && goldMyInviteData.size() > 0) {
				totalReward = (BigDecimal) goldMyInviteData.get("total_reward");
				if(totalReward == null) {
					totalReward = new BigDecimal(0);
				}
			}
			view.addObject("totalReward", totalReward);
			
			//我的邀请榜
			view.addObject("inviteInvestRank", slaveBidAnalysisService.findInviteInvestRank(userDO.getId(), sdf.format(activity.getBeginTime()), sdf.format(DateUtils.addDays(activity.getEndTime(), 1))));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/gain_gold_go_invite.htm")
	public String gainGoldGoInvite(HttpServletRequest request){
		JSONObject json = new JSONObject();
		UserDO user = getCurrentUser(request);
		if(user == null) {
			json.put("loginStatus", 0);
		} else {
			json.put("loginStatus", 1);
		}
		return json.toJSONString();
	}

}
